﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_MailSend]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_MailSend];
GO
CREATE PROCEDURE [dbo].[sproc_MailSend]
    (
     @MailFolderType        [numeric],
     @MailReceiverStr     [nvarchar](300),
     @MailSendDate     [datetime],
     @MailSendLevel     [smallint],
     @MailSender         [nvarchar](20),
      @MailReceiver         [nvarchar](20),
    @MailSubject        [nvarchar](30),
     @MailBody            [text],
    @MailCcToAddr     [nvarchar](300),
     @MailBccToAddr     [nvarchar](300),
     @MailReadFlag     [bit],
     @MailTypeFlag     [bit],
              @MailClassID         [int],
              @MailImportance     [int],
              @MailID                         [int] OUTPUT
)
AS 
    -- SET NOCOUNT to ON and no longer display the count message
    SET NOCOUNT ON
    
    DECLARE @CurrentError int
    -- start transaction, inserting into TabMailList
    BEGIN TRANSACTION
INSERT INTO TabMailList
     ( 
     [MailFolderType],
     [MailReceiverStr],
     [MailSendDate],
     [MailSendLevel],
     [MailSender],
      [MailReceiver],
     [MailSubject],
     [MailBody],
     [MailCcToAddr],
     [MailBccToAddr],
     [MailReadFlag],
     [MailTypeFlag],
     [ClassID],
     [MailImportance]) 
 
VALUES 
    ( 
     @MailFolderType,
     @MailReceiverStr,
     @MailSendDate,
     @MailSendLevel,
     @MailSender,
     @MailReceiver,
     @MailSubject,
     @MailBody,
    @MailCcToAddr,
     @MailBccToAddr,
     @MailReadFlag,
     @MailTypeFlag,
     @MailClassID,
     @MailImportance)
select @MailID=@@identity
  IF @CurrentError != 0
        BEGIN
               GOTO ERROR_HANDLER
        END
--insert  end
  
-- end of transaction
    COMMIT TRANSACTION
    -- Reset SET NOCOUNT to OFF
    SET NOCOUNT OFF
    -- return 0 to indicate success, otherwise the raised error will be returned
    RETURN 0
  ERROR_HANDLER:
        ROLLBACK TRANSACTION
        SET NOCOUNT OFF
        RETURN @CurrentError